package com.erp.myAdapter;

import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/2 0002.
 * 作者：刘洋
 */
public class ClientPagerAdapter extends FragmentPagerAdapter {
    private List<Fragment> listfragment=new ArrayList<>();
    public ClientPagerAdapter(FragmentManager fm, List<Fragment> listfragment) {
        super(fm);
        this.listfragment=listfragment;
    }
    @Override
    public Fragment getItem(int position) {
        return listfragment.get(position);
    }

    @Override
    public int getCount() {
        return listfragment.size();
    }
    public void addFragment(Fragment fragment) {
        listfragment.add(fragment);
    }

    public void removeFragment(Fragment fragment) {
        listfragment.remove(fragment);
        notifyDataSetChanged();
    }

    public void setFragments(List<Fragment> fragments) {
        listfragment = fragments;
    }

    public void clear() {
        for (Fragment fragment : listfragment) {
            if (fragment != null && fragment.isAdded()) {
                fragment.onDestroy();
            }
        }
        listfragment.clear();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        return super.instantiateItem(container, position);
    }

}
